﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC3Demo.Models;
using System.Data.SqlClient;

namespace MVC3Demo.Controllers
{
    public class ItemController : Controller
    {
        private MVC3DemoEntities db = new MVC3DemoEntities();

        private const int PAGE_SIZE = 20;
        //
        // GET: /Item/

        public ViewResult Index(string page, string search)
        {
            ViewBag.PageSize = PAGE_SIZE;

            if (search == null) search = string.Empty;

            List<Item> model =
                db.Items
                .Where(i => i.Message.ToLower().Contains(search))
                .OrderBy(i => i.Id)
                .Skip((int.Parse(page) - 1) * PAGE_SIZE)
                .Take(PAGE_SIZE)
                .ToList();

            //List<Item> model = db.Database.SqlQuery<Item>("EXEC SearchForItems @search", new SqlParameter("search", search))
            //    .OrderBy(i => i.Id)
            //    .Skip((int.Parse(page) - 1) * PAGE_SIZE)
            //    .Take(PAGE_SIZE)
            //    .ToList();

            ViewBag.ItemCount = db.Items.Count(i => i.Message.ToLower().Contains(search));

            if (string.IsNullOrEmpty(page))
            {
                page = "1";
                RouteData.Values["page"] = 1;
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(string search)
        {
            return RedirectToAction("Index", new { page = 1, search = search });
        }

        //
        // GET: /Item/Details/5

        public ViewResult Details(long id)
        {
            Item item = db.Items.Find(id);
            return View(item);
        }

        //
        // GET: /Item/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Item/Create

        [HttpPost]
        public ActionResult Create(Item item)
        {
            if (ModelState.IsValid)
            {
                db.Items.Add(item);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(item);
        }

        //
        // GET: /Item/Edit/5

        public ActionResult Edit(long id)
        {
            Item item = db.Items.Find(id);
            return View(item);
        }

        //
        // POST: /Item/Edit/5

        [HttpPost]
        public ActionResult Edit(Item item)
        {
            if (ModelState.IsValid)
            {
                db.Entry(item).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(item);
        }

        //
        // GET: /Item/Delete/5

        public ActionResult Delete(long id)
        {
            Item item = db.Items.Find(id);
            return View(item);
        }

        //
        // POST: /Item/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {
            Item item = db.Items.Find(id);
            db.Items.Remove(item);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}